<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>LNMP常见的502错误 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>LNMP常见的502错误</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LNMP/">LNMP</a> <a class="article__tag-link" href="/tags/Nginx/">Nginx</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于 LNMP 来说，最常见的问题就是502问题了。配置完环境后，一访问网站直接提示“502 Bad Gateway”。出现502的原因大致分为两种</p>
<ol>
<li>配置错误</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为nginx找不到php-fpm了，所以报错，一般是fastcgi_pass后面的路径配置错误了，后面可以是socket或者是ip:port</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 nginx 中有配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">location ~ \.php$ &#123;</div><div class="line">      include fastcgi_params;</div><div class="line">      fastcgi_pass unix:/tmp/php-fcgi.sock;</div><div class="line">      fastcgi_index index.php;</div><div class="line">      fastcgi_param SCRIPT_FILENAME /usr/<span class="built_in">local</span>/nginx/html<span class="variable">$fastcgi_script_name</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果把 fastcgi_pass 后面指定路径配置错了，那么就会出现502错误，因为 nginx 找不到 php-fpm 了。fastcgi_pass 后面可以跟 socket 也可以跟 ip：port，默认监听地址为 127.0.0.1：9000.</p>
<ol>
<li>资源耗尽</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;lnmp架构在处理php时，nginx直接调取后端的php-fpm服务，如果nginx的请求量偏高，我们又没有给php-fpm配置足够的子进程，那么php-fpm就会资源耗尽，一旦资源耗尽nginx找不到php-fpm就会出现502错误，</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决方案：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;去调整php-fpm.conf中的pm.max_children数值，使其增加，但是也不能无限增加，毕竟资源有限，一般4G内存机器如果跑php-fpm和nginx，不跑mysql可以设置为150，8G为300以此类推</p>
<ol>
<li>除了上面的两种错误还有其他的原因，很少有，我们可以借助nginx的错误日志来进行排查</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim /usr/<span class="built_in">local</span>/nginx/logs/nginx_error.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们也可以给日志定义级别vim/usr/local/nginx/conf/nginx.conf 找到error_log，默认是crit最严谨的就行，也可以改成debug显示的信息最全面，但是很容易撑爆我们的磁盘。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先我们需要让浏览器进行访问</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改nginx的配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">[root@wqslinux ~]<span class="comment"># vim/usr/local/nginx/conf/vhosts/111.conf</span></div><div class="line"> </div><div class="line">server</div><div class="line">&#123;</div><div class="line"> listen 80;</div><div class="line"> server_name www.111.com; <span class="comment">#域名地址</span></div><div class="line"> index index.html index.htm index.php;</div><div class="line"> root /data/www/;</div><div class="line"> </div><div class="line"> location ~ \.php$ &#123;</div><div class="line"> include fastcgi_params;</div><div class="line"> fastcgi_pass unix:/tmp/www.sock; //修改sock</div><div class="line"> <span class="comment">#fastcgi_pass 127.0.0.1:9000;</span></div><div class="line"> fastcgi_index index.php;</div><div class="line"> fastcgi_param SCRIPT_FILENAME /data/www<span class="variable">$fastcgi_script_name</span>;</div><div class="line"> &#125;</div><div class="line"> </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检查语法是否正常</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@wqslinux ~]<span class="comment">#/usr/local/nginx/sbin/nginx -t</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重新加载配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@wqslinux ~]<span class="comment"># /usr/local/nginx/sbin/nginx-s reload</span></div><div class="line">[root@wqslinux ~]<span class="comment"># /etc/init.d/nginx reload</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检查nginx是那个用户跑的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@wqslinux ~]<span class="comment"># ps aux |grep nginx</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑php-fpm文件</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们要在这个php-fpm文件里面设置nginx的用户主，跟组这样才不会显示502</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">[root@wqslinux ~]<span class="comment"># vim/usr/local/php/etc/php-fpm.conf</span></div><div class="line"></div><div class="line"></div><div class="line">[global]</div><div class="line">pid = /usr/<span class="built_in">local</span>/php/var/run/php-fpm.pid</div><div class="line">error_log =/usr/<span class="built_in">local</span>/php/var/<span class="built_in">log</span>/php-fpm.log</div><div class="line">[www]</div><div class="line">listen = /tmp/www.sock</div><div class="line">user = php-fpm</div><div class="line">group = php-fpm</div><div class="line">listen.owner = nobody //定义属主</div><div class="line">listen.group = nobody //定义属组</div><div class="line">pm = dynamic</div><div class="line">pm.max_children = 50</div><div class="line">pm.start_servers = 20</div><div class="line">pm.min_spare_servers = 5</div><div class="line">pm.max_spare_servers = 35</div><div class="line">pm.max_requests = 500</div><div class="line">rlimit_files = 1024</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置完之后重启php-fpm</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@wqslinux ~]<span class="comment"># /etc/init.d/php-fpm restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ps： 再补充一个，是近期遇到的问题</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这种情况下，<strong>使用的是socket，版本高于5.4（含5.4） 默认监听的socket文件权限是所有者只读，属组和其他用户没有任何权限</strong>。所以，nginx的启动用户（咱们配置的是 nobody）就没有办法去读这个socket文件，最终导致502，这个问题可以在nginx的错误 日志中发现。解决办法很简单，上面给出的配置文件中就有避免这个问题的配置。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">listen.owner = nobody    <span class="comment">#定义属主</span></div><div class="line">listen.group = nobody    <span class="comment">#定义属组</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这两个配置就是定义socket的属主和属组是谁。除了这个还有一种方法</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">listen.mode = 777</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样nobody也可以有读取权限了。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Nginx/12. LNMP常见的502错误/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
